﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#pragma once

#include "ILevel.h"
#include "IPlane.h"
#include "NdbObjectSchematic.h"
#include "ILevelHeaderBehavior.h"
#include "IElementParametersCustomizer.h"
#include "IElementUiManipulatorComponent.h"
#include "IElementViewSpecificShapeComponent.h"
#include "IElementShapeHandleBehavior.h"
#include "IElementDeletionComponent.h"
#include "IElementTransformationComponent.h"
#include "WeakPtr.h"
#include "IElement.h"
#include "IElementCopyPasteComponent.h"
#include "IModelViewEventHandler.h"

namespace gcmp {

    class IElement;
    class IDocument;
    class IGraphicsElementShape;
    class IModelView;
    class IElementShapeHandle;
    class ElementViewSpecificShapeComponent;

    //////////////////////////////////////////////////////////////////////////
    // NewLevel的ModelView定制行为
    class NewLevelViewSpecificShapeComponent : public NdbObjectSchematic,
        public IElementViewSpecificShapeComponent
    {
        DBOBJECT_DATA_BEGIN_AND_QUICK_CAST_FROM(gcmp, NewLevelViewSpecificShapeComponent, gcmp::NdbObjectSchematic, A6F7E11F - 9866 - 412F - 888F - 675A56875DBC, gbmpmodel, IElementViewSpecificShapeComponent)
            DATA(OwnerPtr<IRenderPriorityOffsetProvider>, RenderPriorityOffsetProvider)
            DATA_TRANSIENT(IElement*, pOwnerElement)
            DATA(OwnerPtr<IGraphicsNodeVisibilityCustomizer>, GraphicsNodeVisibilityCustomizer)
        DBOBJECT_DATA_END
    public:
        virtual bool IsVisibleInModelView(const IModelView* pModelView) const override;
        virtual OwnerPtr<IGraphicsElementShape> CreateViewSpecificElementShape(const IModelView* pModelView) const override;
        virtual bool IsCuttableByModelViewRange(const IModelView* pModelView) const override;
        virtual void ReportDependentElements(std::vector<ElementId>& dependentElementIds) const override {};
        bool NeedsRecreateViewSpecificElementShape(const IModelView* pModelView, const RecreateShapeInfo& shapeInfo) const override;

        virtual void SetRenderPriorityOffsetProvider(OwnerPtr<IRenderPriorityOffsetProvider> opProvider) override;
        virtual const IRenderPriorityOffsetProvider* GetRenderPriorityOffsetProvider() const override;
        virtual IRenderPriorityOffsetProvider* GetRenderPriorityOffsetProvider() override;

        virtual void SetGraphicsNodeVisibilityCustomizer(OwnerPtr<IGraphicsNodeVisibilityCustomizer> opCustomizer) override;
        virtual const IGraphicsNodeVisibilityCustomizer* GetGraphicsNodeVisibilityCustomizer() const override;
        virtual IGraphicsNodeVisibilityCustomizer* GetGraphicsNodeVisibilityCustomizer() override;
    public:
        virtual bool SetOwnerElement(IElement* pOwnerElement) override;
        virtual IElement* GetOwnerElement() override;
        virtual const IElement* GetOwnerElement() const override;
    public: //实现NdbObject接口
        virtual NdbObject* GetTopOwnerObject() const override
        {
            return nullptr;
        }
    };

    //////////////////////////////////////////////////////////
    // NewLevel的CustomizableParameter定制行为
    class NewLevelParametersCustomizer : public NdbObjectSchematic, public gcmp::IElementParametersCustomizer
    {
        DBOBJECT_DATA_BEGIN_AND_QUICK_CAST_FROM(gcmp, NewLevelParametersCustomizer, gcmp::NdbObjectSchematic, 8831883C-C5C1-4605-96D1-F8B594521784, gbmpmodel, IElementParametersCustomizer)
        DBOBJECT_DATA_END
    public:
        NewLevelParametersCustomizer(const IElement* pElement);

        virtual OwnerPtr<IParameter> GetNativeParameter(int paramDefId) const override;
        virtual bool SetNativeParameter(const IParameter *param, std::wstring* errorMsg) override;
        void ReportParameterDefinitions(std::vector<int>* pParamDefIds) const override;
        virtual bool IsParameterModifiable(int parameterId) const override;
        virtual bool IsPresetParameter(const UniIdentity& paramDefUid) const override { return true; }
        virtual void FilterParameterValues(const UniIdentity& paramDefUid, 
            std::vector<OwnerPtr<IParameterValueStorage>>& values) const override {}

    public:
        virtual bool SetOwnerElement(IElement* pOwnerElement) override;
        virtual IElement* GetOwnerElement() override;
        virtual const IElement* GetOwnerElement() const override;
    public: //实现NdbObject接口
        virtual NdbObject* GetTopOwnerObject() const override
        {
            return nullptr;
        }

    private:
        IDocument* GetDocument() const;
    private:
        IElement* m_pOwnerElement;
    };

    //////////////////////////////////////////////////////////
    // NewLevel的ElementUiManipulatorComponent定制行为
    class NewLevelUiManipulatorComponent : public NdbObjectSchematic, public IElementUiManipulatorComponent
    {
        DBOBJECT_DATA_BEGIN_AND_QUICK_CAST_FROM(gcmp, NewLevelUiManipulatorComponent, gcmp::NdbObjectSchematic, BC01F87C - 3022 - 4669 - B03D - E3E0B457B355, gbmpmodel, IElementUiManipulatorComponent)
        DBOBJECT_DATA_END
    public:
        virtual bool SetOwnerElement(IElement* pOwnerElement) override;
        virtual IElement* GetOwnerElement() override;
        virtual const IElement* GetOwnerElement() const override;
    public: //实现NdbObject接口
        virtual NdbObject* GetTopOwnerObject() const override
        {
            return nullptr;
        }
    private:
        IElement* m_pOwnerElement;
    };

    enum class NewLevelShapeElementPosition : uint8_t // 辅助点在标高的位置
    {
        Left = 0,
        Right = 1
    };

    //////////////////////////////////////////////////////////
    // NewLevel的ElementShapeHandleBehavior定制行为
    class GBMP_MODEL_EXPORT NewLevelShapeHandleBehavior :public NdbObjectSchematic, public IElementShapeHandleBehavior
    {
        DBOBJECT_DATA_BEGIN_AND_QUICK_CAST_FROM(gcmp, NewLevelShapeHandleBehavior, gcmp::NdbObjectSchematic, DD062FF7 - B7AC - 478B - 8299 - B1B215298CDA, gbmpmodel, gcmp::IElementShapeHandleBehavior)
            DATA(IDocument*, document)
            DATA(NewLevelShapeElementPosition, Position)
            DATA(ElementId, ElementShapeHandleId)
        DBOBJECT_DATA_END
    public:
        NewLevelShapeHandleBehavior(IDocument* pDoc, NewLevelShapeElementPosition pt);
        ~NewLevelShapeHandleBehavior();

        //属性存取
    public:
        IDocument* GetDocument() const override { return Getdocument__(); }
        void SetDocument(const IDocument* pDoc) { Setdocument__(pDoc); }

        NewLevelShapeElementPosition GetPosition() const { return GetPosition__(); }
        void SetPosition(NewLevelShapeElementPosition pt) { SetPosition__(pt); }

        ElementId GetElementShapeHandleId() const override { return GetElementShapeHandleId__(); }
        void SetElementShapeHandleId(const ElementId& id) override { return SetElementShapeHandleId__(id); }
        IElementShapeHandle* GetElementShapeHandle() const;

        //重写IElementShapeHandleBehavior方法
    public:
        virtual ElementId GetModelViewId() const override { return ElementId::InvalidID; };
        virtual bool Translate(const gcmp::IElementMoveContext& moveContext) override { return false; }
        virtual void GetCalculators(ICalculatorCollection* calculators) const override;
        virtual void UpdatePosition() override {};
        virtual void ReportParents(IElementParentReporter& reporter) const override {};
        virtual void GetNearestPoint(Vector3d& nearPoint) const override {};
        virtual gcmp::OwnerPtr<gcmp::IGraphicsElementShape> CreateGrahicsElementShapeInModelView(const gcmp::IModelView* pModelView) const override;
        virtual bool FinishEditing() override { return false; }
    public: //实现NdbObject接口
        virtual NdbObject* GetTopOwnerObject() const override
        {
            return nullptr;
        }

    public:
        virtual bool GetHandleCommandString(const IObject* commandEnvironment, std::wstring& commandStr, gcmp::CommandParameters& cmdParams) const override;
        virtual void UpdatePositionIndexInMasterElement() override;
    };

    //////////////////////////////////////////////////////////
    // NewLevel的ElementDeletionComponent定制行为
    class NewLevelDeletionComponent : public NdbObjectSchematic, public IElementDeletionComponent
    {
        DBOBJECT_DATA_BEGIN_AND_QUICK_CAST_FROM(gcmp, NewLevelDeletionComponent, gcmp::NdbObjectSchematic, 7B665208 - D7BE - 4278 - 8FF5 - 541BE853476C, gbmpmodel, IElementDeletionComponent)
        DBOBJECT_DATA_END

    public:
        virtual bool SetOwnerElement(IElement* pOwnerElement) override;
        virtual IElement* GetOwnerElement() override;
        virtual const IElement* GetOwnerElement() const override;

    public: //实现NdbObject接口
        virtual NdbObject* GetTopOwnerObject() const override
        {
            return nullptr;
        }

    public:
        virtual bool CanBeDeleted(const IElementDeletionContext* pDeleteContext, std::wstring* explanation) const override;
        virtual bool GetDeletionWarning(std::vector<std::wstring>& strWarning) const override;

    private:
        IElement* m_pOwnerElement;
    };

    class NewLevelTransformationComponent : public NdbObjectSchematic, public IElementTransformationComponent
    {
        DBOBJECT_DATA_BEGIN(gcmp, NewLevelTransformationComponent, gcmp::NdbObjectSchematic, 4F09859E - 8F30 - 417C - 9BC4 - 99A496E96AF8, gbmpmodel)
        DBOBJECT_DATA_END
    public:
        virtual IElement* GetOwnerElement() override;
        virtual const IElement* GetOwnerElement() const override;
        virtual bool SetOwnerElement(IElement* pOwnerElement) override;

#pragma region 通用操作
        virtual bool Translate(const IElementMoveContext& moveContext) override;
        virtual bool Rotate(const Vector3d& origin, const Vector3d& axis, double angle) override;
        virtual bool Transform(const Matrix4d& matrix) override;
        virtual bool CanBeMoved(std::wstring* explanation = nullptr) const override;
        virtual bool CanBeRotated(std::wstring* explanation = nullptr) const override;
        virtual bool CanBeScaled(std::wstring* explanation = nullptr) const override { return false; }
        virtual bool Scale(const Vector3d& basePt, float scaleFactor) override { return false; }
#pragma endregion 通用操作
    public: //实现NdbObject接口
        virtual NdbObject* GetTopOwnerObject() const override
        {
            return nullptr;
        }
    private:
        IElement* m_pOwnerElement;
    };

    // 拷贝行为
    class NewLevelCopyPasteComponent : public NdbObjectSchematic, public IElementCopyPasteComponent
    {
        DBOBJECT_DATA_BEGIN_AND_QUICK_CAST_FROM(gcmp,NewLevelCopyPasteComponent,gcmp::NdbObjectSchematic, 57F54D79 - B617 - 4F15 - 9508 - E76E7A469BF5, gbmpmodel, IElementCopyPasteComponent)
        DBOBJECT_DATA_END
    public:
        explicit NewLevelCopyPasteComponent(IElement* pOwnerElement)
            : m_pOwnerElement(pOwnerElement)
        {}
    public:
        virtual OwnerPtr<IPasteHelper> GetPasteHelper() const override { return nullptr; }
        virtual void UpdateAfterCopy(const ICopyContext& copyContext) override;
        virtual void UpdateForWeakParentCopied(const IDocument* pDocFrom, const IElement* pSource, const IElement* pCopy) const override;
        virtual bool ShouldBeCopiedTogether(const ICopyContext & copyContext) const override;
        virtual bool IsStrongParentsNeedCopied() const override;
        virtual bool CanBeCopied(std::wstring* pErrorMessage) const override;

    public:
        virtual bool SetOwnerElement(IElement* pOwnerElement) override { m_pOwnerElement = pOwnerElement; return true; }
        virtual IElement* GetOwnerElement() override { return m_pOwnerElement; }
        virtual const IElement* GetOwnerElement() const override { return m_pOwnerElement; }

        // 辅助方法
        IDocument* GetDocument() const;
        ElementId GetOwnerElementId() const;
    public: //实现NdbObject接口
        virtual NdbObject* GetTopOwnerObject() const override
        {
            return nullptr;
        }
    private:
        IElement* m_pOwnerElement;
    };

    class NewLevelCopyStrategyComponent : public NdbObjectSchematic, public IElementCopyStrategyComponent
    {
        DBOBJECT_DATA_BEGIN_AND_QUICK_CAST_FROM(gcmp, NewLevelCopyStrategyComponent, gcmp::NdbObjectSchematic, 55392113 - 6FF8 - 4376 - 953D - D002020E73B8, gbmpmodel, IElementCopyStrategyComponent)
        DBOBJECT_DATA_END

    public:
        virtual void Report(const gcmp::ICopyContext & copyContext, gcmp::ICopyStrategyReporter& reporter) const override;
    public:
        virtual bool SetOwnerElement(IElement* pOwnerElement) override { m_pOwnerElement = pOwnerElement; return true; }
        virtual IElement* GetOwnerElement() override { return m_pOwnerElement; }
        virtual const IElement* GetOwnerElement() const override { return m_pOwnerElement; }

        // 辅助方法
        IDocument* GetDocument() const;
        ElementId GetOwnerElementId() const;

    public: //实现NdbObject接口
        virtual NdbObject* GetTopOwnerObject() const override
        {
            return nullptr;
        }

    private:
        IElement* m_pOwnerElement;
    };
}// namespace gcmp

